package DynamicProgramming._303_RangeSumQuery_Immutable;

public class NumArray {
    private int[] nums;
    private int[] sums;
    public NumArray(int[] nums) {
        this.nums = nums;
        this.sums = new int[nums.length];
        if (nums.length != 0) {
            sums[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                sums[i] = nums[i] + sums[i - 1];
            }
        }
    }

    //solution 2:use dynamic programming
    public int sumRange(int i, int j) {
        return sums[j] - sums[i] + nums[i];
    }

    //solution 1:bruced force:over time limited
    public int sumRange_1(int i, int j) {
        int sum = 0;
        for(int ii = i;ii<=j;ii++) {
            sum += nums[ii];
        }
        return sum;
    }


}
